// basicnpc.txt
// A very simple, naive text. Creature attacks anything it hates nearby.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.

begincreaturescript;

variables;

short i,target,dialog;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);

	set_name(ME,"Barbarus");

	break;

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		set_flag(get_memory_cell(1),get_memory_cell(2),1);
break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (select_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		do_attack();
		set_state(3);
		}
		
	// Otherwise, just peacefully move around. Go back to start, if I'm too far
	// from where I started.
	if ((my_dist_from_start() >= 6) || ((my_dist_from_start() > 0) && (get_memory_cell(0) > 0))) {
		if (get_ran(1,1,100) < 40) 
			return_to_start(ME,1);
		}
		else if (get_memory_cell(0) == 0) {
			fidget(ME,25);
			}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate TALKING_STATE;

	if(get_flag(20,1) == 2){
		reset_dialog();
		add_dialog_str(0,"Barbarus turns his head to you.  _What do you need?  Have you come to me for guidance?_",0);
		add_dialog_str(1,"You explain to him that you found Chief Licgan's axe in the hands of the Goblin Chief whom you slew, and that somebody using a Claymore was the one who slew Wihcaser.",0);
		add_dialog_str(2,"_A Claymore-wielding member of our tribe slew Wihcaser?  Not many people use Claymores, they prefer Pikes.  I can see how you would suspect something ill.  As you know, our Chief uses a Claymore._",0);
		add_dialog_str(3,"_I suggest that you confront Chief Licgan with the evidence.  I can think of many things that could be wrong, but it might just be a coincidence._",0);
		add_dialog_str(4,"_Yes...  Confront Chief Licgan with this evidence.  That is my advice to you._",0);
		add_dialog_choice(0,"Yes sir.");
		dialog = run_dialog(1);
		if(get_flag(2,1) < 76){
			message_dialog("Barbarus pauses.  _But Speres...  Your mission takes precedence.  Please, do the tribe a favor and complete your mission before confronting Chief Licgan._","");
		}
		set_flag(20,1,3);
		end();
	}
	if(get_flag(20,1) > 2){
		message_dialog("_I have given you my advice...  Confront Chief Licgan with the evidence you have.  Maybe then, you will know the truth.  That is all._","");
		if(get_flag(2,1) < 76){
			message_dialog("Barbarus pauses.  _But Speres...  Your mission takes precedence.  Please, do the tribe a favor and complete your mission before confronting Chief Licgan._","");
		}
		end();
	}

	if(get_flag(2,1) >= 53 && get_flag(20,1) < 1){
		message_dialog("Barbarus shakes his head.  _Don't worry, I trust that you have been taught enough that you would know what to do.  You have been told the right way._","_Do as you must.  Which is to say, do as you are told.  Do not let a horrible mistake like this happen again._");
		end();
	}
	if(get_flag(2,1) == 52){
		reset_dialog();
		add_dialog_str(0,"You begin to tell Wiseman Barbarus of your problems, but he raises his wrinkled hand.  _Enough.  I know why you are here._",0);
		add_dialog_str(1,"_The spirits came to me and told me of Wihcaser's wrongdoings.  Regardless of the circumstances which forced Wihcaser to do what he did, Licgar was right to banish him._",0);
		add_dialog_str(2,"_I suspect that something has gone awry with Wihcaser.  He has never been a faithless child, nor has he ever been one to go on such wild adventures._",0);
		add_dialog_str(3,"_Still, I felt that something had gone amiss when Wihcaser consulted me three days ago.  He asked me what the authority in his life should be, but told me nothing of why he had visited me._",0);
		add_dialog_str(4,"_I told him, and perhaps falsely so, that he should trust his instincts.  I believed him to be a faithful child, but perhaps I empowered him too broadly._",0);
		add_dialog_choice(0,"Are you suggesting that he did this out of greed?");
		dialog = run_dialog(1);

		reset_dialog();
		if(party_size() > 1){
			add_dialog_str(0,"_I might, I might not.  It matters not.  I made the mistake once, I will not make it again.  Speres, listen well._",0);
		}
		else{
			add_dialog_str(0,"_I might, I might not.  It matters not.  I made the mistake once, I will not make it again.  Spere, listen well._",0);
		}
		add_dialog_str(1,"_Trust solid word.  I told Wihcaser to trust his instincts, and so he did.  His instincts were not the way he was raised, and I blame myself for the Aquos Gem's disappearance._",0);
		add_dialog_str(2,"_You were told by Chief Licgan, and rightfully so, to retrieve the Aquos Gem from Wihcaser by any means necessary.  Those are your orders._",0);
		add_dialog_str(3,"_Do as you must, but be sure to retrieve that gem no matter what.  You have been taught well, so I expect that you will not let your people down._",0);
		add_dialog_choice(0,"Yes sir.");
		dialog = run_dialog(1);
		set_flag(2,1,53);
		end();
	}

break;